<?php

	class module_module_manager extends tpl_module {
		public function __construct(){
			$this->action_list = array(
				"add_module"
			);
		}
		public function __default(){
			if(!$project_id  = $this->get_param("project_id")){
				$this->set_page("project_manager");
			}
			
			
			$index = $this->create_index();
			$module_index = $this->create_module_index($project_id);
			#context::get("io")->dump($index);
			
			$this->tpl->assign("projects",$index);
			$this->tpl->assign("modules",$module_index);
			$this->tpl->assign("project_id",$project_id);
			
		}
		
		public function __add_module(){
			if (!$module_name = $this->get_param("module_name")){
				return $this->notify_error("No module name specified");
			}
			if (!$project_id = $this->get_param("project_id")){
				return $this->notify_error("No project name specified");
			}
			
			$index = $this->create_module_index($project_id);
			if (!isset($index[$module_name])){
				$this->create_new_module($project_id, $module_name);
			}else{
				return $this->notify_error("Duplicate module name '{$module_name}'");
			}
			

			//header("Location: ?");
		}
		
		private function create_index(){
			$index = array();
			$root = $this->get_core_root()._DRS."..";

			
			foreach(new DirectoryIterator($root) as $file){
				if (!$file->isDot() && $file->isDir()){
					if (is_file($file->getPathname()._DRS."{$file}.prj.php")){
						$index[$file->getFilename()] = $file->getPathname();
					}
				}
			}
			return $index;
		}
		
		private function create_module_index($project_id){
			$index = array();
			$root = $this->get_core_root()._DRS.".."._DRS . $project_id . _DRS."modules";

			foreach(new DirectoryIterator($root) as $file){
				if (!$file->isDot() && $file->isDir()){
					if (is_file($file->getPathname()._DRS."{$file}.module.php")){
						$index[$file->getFilename()] = $file->getPathname();
					}
				}
			}

			return $index;
		}
		
		private function create_new_module($project_id, $module_name){
			$this->msg("creating module '{$module_name}'");
			
			$root = $this->get_root();
			$dest_path = $this->get_core_root()._DRS.".."._DRS . $project_id . _DRS."modules";
			
			mkdir($dest_path._DRS.$module_name, 0777, true);	//required for correct rename

			$res_path = $root._DRS."resources"._DRS.$this->get_name()._DRS."new_module";
			if (!UTIL_copy_recursive(
				$res_path,
				$dest_path,
				array('folderPermission'=>0777,'filePermission'=>0777),
				array($this,"proc_new_module_file")
			)){
				$this->msg("failed to copy folder '{$res_path}' to '{$dest_path}'");
			}
			
			rmdir($dest_path._DRS."%module_name%");
		}
		
		public function proc_new_module_file($file_name){
			#$this->msg($file_name);
			
			//parse file name
			$file_name_parsed = $this->parse_string($file_name);
			if ($file_name_parsed != $file_name){
				rename($file_name, $file_name_parsed);
			}
			
			//parse file template
			$content = file_get_contents($file_name_parsed);
			$content = $this->parse_string($content);
			file_put_contents($file_name_parsed,$content);
		}
		
		private function parse_string($string){
			$var_list = array(
				"%module_name%"
			);
			foreach ($var_list as $var){
				$string = str_replace($var,$this->parse_token($var),$string);
			}
			return $string;
		}
		
		private function parse_token($token){
			switch($token){
				case "%module_name%":
					return $this->get_param("module_name");
				break;
				
				default:
					return $token;
				break;
			}
		}

	}
?>
